package cn.stu.projectsummit.user.controller;


import cn.stu.projectsummit.base.response.JsonResult;
import cn.stu.projectsummit.user.pojo.dto.UserLoginParam;
import cn.stu.projectsummit.user.pojo.dto.UserQuery;
import cn.stu.projectsummit.user.pojo.dto.UserSaveParam;
import cn.stu.projectsummit.user.pojo.vo.UserVO;
import cn.stu.projectsummit.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("v1/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("select")
    public JsonResult select(UserQuery userQuery) {
        log.debug("用户查询业务:userQuery:{}", userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("save")
    // 之前前端发过来的是JSON格式的JS对象,需要使用@RequestBody注解
    // 将前端发过来的JSON格式的JS对象转换为UserSaveParam对象
    // 以后统一去掉@RequestBody注解,数据由前端处理好再发送到Controller
    // public JsonResult saveUser(@RequestBody UserSaveParam userSaveParam){
    public JsonResult saveUser(UserSaveParam userSaveParam) {
        log.debug("用户保存:userSaveParam={}", userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @PostMapping("/update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId) {
        log.debug("密码重置:userId={}", userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @PostMapping("delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId) {
        log.debug("删除用户:userId={}", userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam) {
        log.debug("用户登录:userLoginParam={}", userLoginParam);
        //调用业务层方法,如果业务层方法成功执行完毕,可以拿到业务层返回的usrVO
        UserVO userVO = userService.login(userLoginParam);
        //控制层向前端返回登录成功后的完整用户信息userVO
        //将查到的userVO装到.ok()中一起返回给前端
        return JsonResult.ok(userVO);
    }
}
